const route = require('koa-router')();
const bill = require('../../model/bill/bill.js')
const format = require('../../utils/formatTime.js')

route.post('/bill/add_bill',async(ctx,next)=>{
    let reqData = ctx.request.body

    if(!reqData.addDate){
        ctx.body = {
            code:400,
            msg:'请选择时间'
        }
    }else if(!reqData.desc){
        ctx.body = {
            code:400,
            msg:'请填写备注'
        }
    }else if(!reqData.amount){
        ctx.body = {
            code:400,
            msg:'请填写备注'
        }
    }else if(!reqData.bill_type){
        ctx.body = {
            code:400,
            msg:'type不能为空'
        }
    }else if(!reqData.class_id){
        ctx.body = {
            code:400,
            msg:'请选择账单类别'
        }
    }else{
        let all = await bill.find()
        if(all.length==0){//如果在添加时没有账单
            reqData.b_id = 1//第一位用户初始化userid为100000
        }else{//如果有用户
            reqData.b_id = all[all.length-1].b_id+1//取数据库用户表中最后一位用户userid，然后+1
        }
        reqData.addTime = new format(new Date(reqData.addDate)).getTime()
        reqData.time_for_week = new format(new Date(reqData.addDate)).getWeek()
        reqData.at_week = new format(new Date(reqData.addDate)).theWeek()
        reqData.at_month = String(new Date(reqData.addDate).getMonth()+1)
        reqData.at_year = String(new Date(reqData.addDate).getFullYear())
        reqData.at_day = String(new Date(reqData.addDate).getDate())
        await bill.create(reqData)
        ctx.body = {
            code:200,
            msg:'ok'
        }
    }
    // let userList = await adminClass.find({})
    
})

module.exports = route